package jinyilw.swing.rlaf.ui;

import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;
import jinyilw.swing.rlaf.utils.RBorderFactory.EmptyBorder;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JList;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicLabelUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;

/**
 * @author Jinyi
 */
public class RListRenderer extends DefaultListCellRenderer
{
	private static final long serialVersionUID = 7057618122981294505L;
	private static final Border border = new EmptyBorder(2, 3, 2, 2);
	private static final int arc = 10;
	private boolean isSelected;
	private final RenderingConfig renderingConfig = new RenderingConfig();

	public RListRenderer()
	{
		setOpaque(false);
		setBorder(border);
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus)
	{
		setFont(list.getFont());
		setForeground(RThemeColor.colorSet.textColor);
		this.isSelected = isSelected;

		if (value instanceof Icon)
		{
			setIcon((Icon) value);
			setText("");
		} else
		{
			setIcon(null);
			setText((value == null) ? "" : value.toString());
		}
		return this;
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;
		renderingConfig.setAllAAPainting(g2d);
		super.paintComponent(g);
		if (isSelected)
		{

			Color saveColor = g.getColor();
			Stroke saveStroke = g2d.getStroke();
			// g2d.setStroke(GraphicsTools.getLargeStroke());
			g.setColor(RThemeColor.themeColor);
			g.drawRoundRect(1, 1, getWidth() - 2, getHeight() - 2, arc, arc);
			g.setColor(saveColor);
			g2d.setStroke(saveStroke);
		}
		renderingConfig.resetAllAAPainting(g2d);
	}

	@Override
	public void updateUI()
	{
		setUI(new BasicLabelUI());
	}
}